TemporaryEnchantFrame:SetPoint('TOPRIGHT', UIParent, 'TOPRIGHT', -20, -20)

BUFFS_PER_ROW = 12
BUFF_MIN_ALPHA = .5

--------------------------------------
--	CONFIG END
--------------------------------------


local texture = "Interface\\AddOns\\eMedia\\Button"


local function AuraTexture(buttonName, index)
	if not index then return end
	
	local buffName = buttonName..index
	local frame = getglobal(buffName)
	
	if getglobal(buffName..'Gloss') or (not frame) then return end
	
	local icon = getglobal(buffName..'Icon')
	local gloss = getglobal(buffName..'Gloss')
	local border = getglobal(buffName..'Border')
	local duration = getglobal(buffName..'Duration')
	
	frame.total = 0
	frame.updateDura = true
	hooksecurefunc(frame, 'Show', function(self)
		frame.total = 0
	end)
	
	gloss = frame:CreateTexture(buffName..'Gloss','ARTWORK')
	gloss:SetTexture(texture)
	gloss:SetPoint('TOPLEFT', -2, 2)
	gloss:SetPoint('BOTTOMRIGHT', 2, -2)
	--gloss:SetVertexColor(.25,.25,.25)
	
	border = CreateFrame("Frame", nil, frame)
	border:SetPoint("TOPLEFT", frame, "TOPLEFT", -3.5, 3)
	border:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", 4, -3.5)
	border:SetFrameStrata("BACKGROUND")
	border:SetBackdrop {
		edgeFile = "Interface\\AddOns\\eMedia\\glowTex", edgeSize = 5,
		insets = {left = 3, right = 3, top = 3, bottom = 3}
	}
	border:SetBackdropColor(0, 0, 0, 0)
	border:SetBackdropBorderColor(0, 0, 0)
	--border:Hide()
	border.Show = function() end
	
	hooksecurefunc(duration, 'Hide', function(self)
		self:SetText'|cff00ff00N/A|r'
		self:Show()
	end)
	if not duration:IsShown() then duration:Hide() end
end

hooksecurefunc('AuraButton_Update', AuraTexture)
for i = 1, 2 do
	hooksecurefunc(getglobal('TempEnchant'..i), 'Show', function() AuraTexture('TempEnchant', i) end)
end

local function formatTime(timeLeft)
	local min = floor(timeLeft/60)
	local sec = floor(timeLeft%60)
	
	if timeLeft > 600 then
		--return format('|cff00ff00%dm|r', min)
		return format('%dm', min)
	elseif timeLeft > 60 then
		--return format('|cffffd100%d:%02d|r', min, sec)
		return format('%d:%02d', min, sec)
	else
		return format('|cffff0000%ds|r', sec)
	end
end

function AuraButton_UpdateDuration(buffButton, timeLeft)
	if buffButton.updateDura then
		local duration = getglobal(buffButton:GetName()..'Duration')
		if duration then
			duration:SetText(timeLeft and formatTime(timeLeft) or '|cff00ff00N/A|r')
		end
	end
end

local AuraButton_OnUpdate = AuraButton_OnUpdate
function _G.AuraButton_OnUpdate(self, elapsed)
	self.total = self.total - elapsed
	if self.total > 0 then
		self.updateDura = false
	else
		self.total = self.timeLeft - floor(self.timeLeft)
		self.updateDura = true
	end
	
	return AuraButton_OnUpdate(self, elapsed)
end
